import { Component, Output, Input, EventEmitter } from '@angular/core';
import { createFolder_API, createFile_API } from '@/api/files';
//导入公共依赖模块
import {
  ShareModule,
  NzMessageService,
  FormControl,
  FormGroup,
  NonNullableFormBuilder,
  Validators
} from '@app/share/share.module';
import { getStore, getParams } from '@/utils';
@Component({
  selector: 'workSpace-dialog',
  template: `
    <nz-modal
      [(nzVisible)]="isVisible"
      [nzTitle]="dialogTitle"
      (nzOnCancel)="_cancel()"
      (nzOnOk)="_save()"
    >
      <ng-container *nzModalContent>
        <form nz-form [formGroup]="validateForm">
          <nz-form-item>
            <nz-form-control nzErrorTip="请输入名称">
              <nz-input-group>
                <input
                  type="text"
                  nz-input
                  formControlName="foldername"
                  placeholder="请输入名称"
                />
              </nz-input-group>
            </nz-form-control>
          </nz-form-item>
        </form>
      </ng-container>
    </nz-modal>
  `,
  standalone: true,
  imports: [ShareModule]
})
export class DialogComponent {
  @Input() queryParams: any;
  @Output() getList = new EventEmitter<string>();
  isVisible: boolean = false;
  dialogTitle: string = '新增';
  userInfo: any = {};
  row: any = null;
  //表单校验
  validateForm: FormGroup<{
    foldername: FormControl<string>;
  }> = new FormGroup({
    foldername: new FormControl<string>('', [Validators.required])
  });
  constructor(
    private fb: NonNullableFormBuilder,
    private message: NzMessageService
  ) {
    this.fb = fb; //初始化
    this.validateForm = this.fb.group({
      foldername: ['', [Validators.required]]
    });
    this.userInfo = getStore({ name: 'userInfo' });
  }
  _show(title, row: any = {}) {
    this.isVisible = true;
    this.dialogTitle = title;
    this.validateForm = this.fb.group({
      foldername: [row.foldername || '', [Validators.required]]
    });
  }
  //创建文件夹或者excel文件
  _save() {
    if (!this.validateForm.valid) return; //校验
    const userid = this.userInfo.userid;
    const data = Object.assign(this.validateForm.value);
    const folderid = getParams(window.location.search).folderid;
    if (folderid) {
      data['parentfolderid'] = folderid;
    }
    switch (this.dialogTitle) {
      case '新增文件夹':
        createFolder_API(data)
          .then(res => {
            if (res.code !== 200) {
              this.message.create('error', res.msg);
              return;
            }
            this.message.create('success', '创建成功!');
            this._cancel(); //通知父组件更新列表
          })
          .catch(err => {
            console.log('err', err);
          });
        return;
      case '新增Excel':
        data['filesuffix'] = 'xlsx';
        data['filetype'] = 'excel';
        data['filename'] = data.foldername;
        delete data.foldername;
        data;
        createFile_API(data)
          .then(res => {
            if (res.code !== 200) {
              this.message.create('error', res.msg);
              return;
            }
            this.message.create('success', '创建成功!');
            this._cancel(); //通知父组件更新列表
          })
          .catch(err => {
            console.log('err', err);
          });
        return;
    }
  }

  _cancel() {
    const folderid = getParams(window.location.search).folderid;
    //通知父组件更新列表
    this.getList.emit(folderid);
    this.isVisible = false;
  }
}
